package com.campus.customer.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.bean.common.PageUtil;
import com.campus.customer.bean.PointsRecordQueryParam;
import com.campus.customer.entity.PointsRecord;
import com.campus.customer.mapper.PointsRecordMapper;
import com.campus.customer.vo.PointsRecordVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 积分记录表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-04-02
 */
@Service
public class PointsRecordService extends ServiceImpl<PointsRecordMapper, PointsRecord> {

    /**
     * 分页查询积分记录
     *
     * @param queryParam 查询参数
     * @return 分页结果
     */
    public PageInfo<PointsRecordVo> getPointsRecordPage(PointsRecordQueryParam queryParam) {
        PageInfo<PointsRecord> pageInfo = PageHelper.startPage(queryParam.getPageNo(), queryParam.getPageSize())
                .doSelectPageInfo(() -> getPointsRecordList(queryParam));

        List<PointsRecordVo> voList = Lists.newArrayList();
        for (PointsRecord record : pageInfo.getList()) {
            PointsRecordVo vo = new PointsRecordVo();
            BeanUtil.copyProperties(record, vo);
            voList.add(vo);
        }

        return PageUtil.convertPageInfo(pageInfo, voList);
    }

    private List<PointsRecord> getPointsRecordList(PointsRecordQueryParam queryParam) {
        LambdaQueryWrapper<PointsRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(queryParam.getUserId() != null, PointsRecord::getUserId, queryParam.getUserId())
                   .eq(queryParam.getType() != null, PointsRecord::getType, queryParam.getType())
                   .eq(queryParam.getSource() != null, PointsRecord::getSource, queryParam.getSource())
                   .orderByDesc(PointsRecord::getCreateTime);
        
        return this.list(queryWrapper);
    }

}
